package com.example.renyang.happysell.MyUntil;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;

import java.io.File;
import java.io.FileOutputStream;

/**
 * Created by RenYang on 2016/3/20.
 */
public class Tools {

    /**
     *   保存高质量的图片
     *
     */
    public static void compressBitmap(Context context,File outFile,Uri uri){
        try {
            BitmapFactory.Options opt = new BitmapFactory.Options();
            opt.inJustDecodeBounds = true;
            Bitmap bitMap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri),null,opt);
            int width = opt.outWidth;
            int height = opt.outHeight;
            opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
            opt.inPurgeable = true;
            opt.inInputShareable = true;
            opt.inJustDecodeBounds = false;
            opt.inSampleSize = Tools.getFitSample(width, height, 720,720);
            bitMap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri),null,opt);

            FileOutputStream fos = null;
            try{
                fos = new FileOutputStream(outFile);
                if (null != fos) {
                    bitMap.compress(Bitmap.CompressFormat.JPEG, 80, fos);
                    fos.flush();
                    fos.close();
                }
            }finally{
                if(bitMap!=null && !bitMap.isRecycled()){
                    bitMap.recycle();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static int getFitSample(int width, int height, int destWidth, int destHeight){
        int sample = 1;
        while(width>destWidth){
            if(width<destWidth+200){
                break;
            }
            sample = sample*2;
            width = width/sample;
            height = height/sample;
        }

        while(height>destHeight){
            if(height<destHeight+200){
                break;
            }
            sample = sample*2;
            width = width/sample;
            height = height/sample;
        }
        return sample;
    }

    public static int getFitSample(int width, int destWidth){
        int sample = 1;
        while(width>destWidth){
            sample *= 2;
            width = width/sample;
        }
        return sample;
    }

    public static Bitmap getFitBitmap(String path,int destWidth) {
        File file = new File(path);
        if(!file.exists())
            return null;
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeFile(path, opt);

        opt.inSampleSize =getFitSample(opt.outWidth,destWidth);
        opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
        opt.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeFile(path, opt);
        return bitmap;
    }

    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }
}
